﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLKDS.DTO;

namespace QLKDS.DAL
{
    public class CustomerManager
    {
        public CustomerInfo GetCustomerById(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var queryCustomer = from d in db.Customers
                                    where d.Status.Equals(1)
                                    where d.Id.Equals(id)
                                    select new CustomerInfo { Id = d.Id, Code = d.Code,Liabilities=(float)d.Liabilities, Name = d.Name, Status = d.Status, Address = d.Address, Fax = d.Fax, Website = d.Website, TaxCode = d.TaxCode, Mobile = d.Mobile, Phone = d.Phone, Email = d.Email, Description = d.Description, CreatedBy = d.CreatedBy, CreatedDate = d.CreatedDate, ModifiedBy = d.ModifiedBy, ModifiedDate = d.ModifiedDate, Contactor = d.Contactor, ContactorPhone = d.ContactorPhone, ContactorEmail = d.ContactorEmail, ContactorMobile = d.ContactorPhone };

                return queryCustomer.OrderBy(d => d.Code).FirstOrDefault();
            }

        }
        public Customer GetCustomerByCode(string code)
        {
            using (var db = new QLKDSDataContext())
            {
                var queryCustomer = from d in db.Customers
                                    where d.Status.Equals(1)
                                    where d.Code.Equals(code)
                                    select d;

                return queryCustomer.OrderBy(d => d.Code).FirstOrDefault();
            }

        }


        public IList<CustomerInfo> ListCustomer()
        {
            using (var db = new QLKDSDataContext())
            {
                var queryCustomer = from d in db.Customers
                                    where d.Status.Equals(1)
                                    select new CustomerInfo { Id = d.Id, Code = d.Code, Name = d.Name, Status = d.Status, Address = d.Address, Fax = d.Fax, Website = d.Website, TaxCode = d.TaxCode, Mobile = d.Mobile, Phone = d.Phone, Email = d.Email, Description = d.Description, CreatedBy = d.CreatedBy, CreatedDate = d.CreatedDate, ModifiedBy = d.ModifiedBy, ModifiedDate = d.ModifiedDate, Contactor = d.Contactor, ContactorPhone = d.ContactorPhone, ContactorEmail = d.ContactorEmail, ContactorMobile = d.ContactorPhone };

                return queryCustomer.OrderBy(d => d.Code).ToList();
            }
        }

        public void CreateCustomer(Customer customer)
        {
            using (var db = new QLKDSDataContext())
            {
                db.Customers.InsertOnSubmit(customer);

                db.SubmitChanges();

            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="customer"></param>
        public void UpdateCustomer(Customer customer)
        {
            using (var db = new QLKDSDataContext())
            {
                var _customer = db.Customers.Single(d => d.Id.Equals(customer.Id));

                _customer.Code = customer.Code;
                _customer.Name = customer.Name;
                _customer.Liabilities = customer.Liabilities;
                _customer.Address = customer.Address;
                _customer.Mobile = customer.Mobile;
                _customer.Phone = customer.Phone;
                _customer.Email = customer.Email;
                _customer.Status = customer.Status;
                _customer.Fax = customer.Fax;
                _customer.Website = customer.Website;
                _customer.TaxCode = customer.TaxCode;
                _customer.Description = customer.Description;
                _customer.CreatedBy = customer.CreatedBy;
                _customer.CreatedDate = customer.CreatedDate;
                _customer.ModifiedBy = customer.ModifiedBy;
                _customer.ModifiedDate = customer.ModifiedDate;
                _customer.Contactor = customer.Contactor;
                _customer.ContactorEmail = customer.ContactorEmail;
                _customer.ContactorMobile = customer.ContactorMobile;
                _customer.ContactorPhone = customer.ContactorPhone;
                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public void DeleteCustomer(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var _customer = db.Customers.Single(d => d.Id.Equals(id));

                _customer.Status = 0;

                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public void RemoveCustomer(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var _customer = db.Customers.Single(d => d.Id.Equals(id));

                db.Customers.DeleteOnSubmit(_customer);
                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public bool HasCodeExisted(string code)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Customers
                            where d.Code.Equals(code)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="code"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool HasCodeDuplicated(string code, long id)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Customers
                            where d.Code.Equals(code)
                            where !d.Id.Equals(id)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public bool HasMobileExisted(string mobile)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Customers
                            where d.Mobile.Equals(mobile)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mobile"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool HasMobileDuplicated(string mobile, long id)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Customers
                            where d.Mobile.Equals(mobile)
                            where !d.Id.Equals(id)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public bool HasEmailExisted(string email)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Customers
                            where d.Email.Equals(email)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="email"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool HasEmailDuplicated(string email, long id)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.Customers
                            where d.Email.Equals(email)
                            where !d.Id.Equals(id)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }
    }
}
